//
//  YYSpellGroupDetailVC.m
//  drivingSchool
//
//  Created by 子yu 刘 on 2018/8/19.
//  Copyright © 2018年 子yu 刘. All rights reserved.
//

#import "YYSpellGroupDetailVC.h"
#import "YYSpellGroupVC.h"

@interface YYSpellGroupDetailVC ()
@property (nonatomic ,assign)NSInteger lastTime;
@property (nonatomic ,strong) NSDictionary * dataDic;

@end

@implementation YYSpellGroupDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getData];
    self.AllView.layer.borderWidth = 1;
    self.AllView.layer.borderColor = [UIColor colorWithHexString:@"ff5500"].CGColor;
   
}

-(void)getData{
    [RequestOperationManager groupDetail_id:self.carId success:^(id result) {

        if (DEF_REQ_SUCCESS) {
            self.dataDic = [result[@"data"] copy];
            [self reloadData];
        }

    } failture:^(NSError *error) {

    }];
}

-(void)reloadData{
    
    self.lbName.text = [NSString stringWithFormat:@"%@%@",self.dataDic[@"brand"],[NSString getStringIsNotNull:self.dataDic[@"vehicle_type"]]];
    [self.bigImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",DEF_INTERFACE_WEBVIEWSERVICE,self.dataDic[@"vehicle_img"][0]]]];
    self.lbShop.text = self.dataDic[@"seller_name"];
    self.lbAdress.text = self.dataDic[@"seller_address"];
    self.lbPrice.text = self.dataDic[@"money"];
    NSString * price = [NSString stringWithFormat:@"￥%@",self.dataDic[@"benefit_money"]];
    self.lbOldPrice.attributedText = [price changeFontAndColor:[UIFont systemFontOfSize:12] color:self.lbOldPrice.textColor subStringArray:@[@"￥"]];
    
    self.smallViewWidth.constant = 108 * ([self.dataDic[@"current"] floatValue]/[self.dataDic[@"number"] floatValue]);
    self.spellGroupCount.text = [NSString stringWithFormat:@"还剩%i人",[self.dataDic[@"number"] intValue]-[self.dataDic[@"current"] intValue]];
        self.lastTime = [self.dataDic[@"endtime"] integerValue]-[[DateUtil getNowDateWithTime] integerValue];
    self.lbContent.text = self.dataDic[@"content"];
    [self setTime];
    [self addTimer];
    
}

- (IBAction)call:(id)sender {
    NSString *callPhone = [NSString stringWithFormat:@"telprompt://%@",self.dataDic[@"seller_phone"]];
    if (@available(iOS 10.0, *)) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone] options:@{} completionHandler:nil];
    } else {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone]];
    }
}

-(void)addTimer{
    NSTimer *timer  =  [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(numberCutDown:) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

-(void)numberCutDown:(NSTimer *)timer{
    self.lastTime -- ;
    if (self.lastTime == 0) {
        [timer invalidate];
        timer = nil;
       // [self getData];
        return;
    }
    [self setTime];
}
- (void)setTime{
   // NSInteger seconds = self.lastTime % 60;
    NSInteger minutes = (self.lastTime/ 60) % 60;
    NSInteger hours = (self.lastTime / 3600) % 24;
    NSInteger days = self.lastTime / 86400;
    _lbTime.text = [NSString stringWithFormat:@"%.2ld天%.2ld小时%.2ld分",days,hours,minutes];
    
}
- (IBAction)spellGroup:(id)sender {
    if (!DEF_UID) {
        [[AppDelegate shareAppDelegate] enterLogin];
           return;
    }
    YYSpellGroupVC * spell = (YYSpellGroupVC *)[CFUtility getVC:@"YYSpellGroupVC" Storyboard:BUYCAR];
    spell.sid = self.dataDic[@"id"];

    [self.navigationController pushViewController:spell animated:YES];
}

@end
